/*
 * @Description: 创建表单模板的DTO
 */
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsOptional, IsBoolean, IsString, ValidateNested, IsArray } from 'class-validator';
import { Type } from 'class-transformer';
import { FieldType } from '../../../libs/db/src/models/form-template.model';

export class CreateFormFieldDto {
  @ApiProperty({ description: '字段名称' })
  @IsNotEmpty({ message: '字段名称不能为空' })
  @IsString({ message: '字段名称必须为字符串' })
  fieldName: string;

  @ApiProperty({ description: '字段标签' })
  @IsNotEmpty({ message: '字段标签不能为空' })
  @IsString({ message: '字段标签必须为字符串' })
  fieldLabel: string;

  @ApiProperty({ description: '字段描述', required: false })
  @IsOptional()
  @IsString({ message: '字段描述必须为字符串' })
  fieldDescription?: string;

  @ApiProperty({ description: '字段类型', enum: FieldType })
  @IsNotEmpty({ message: '字段类型不能为空' })
  fieldType: FieldType;

  @ApiProperty({ description: '是否必填' })
  @IsBoolean({ message: '是否必填必须为布尔值' })
  required: boolean;

  @ApiProperty({ description: '字段选项（用于select、radio、checkbox类型）', required: false })
  @IsOptional()
  @IsArray({ message: '字段选项必须为数组' })
  @IsString({ each: true, message: '选项必须为字符串' })
  options?: string[];

  @ApiProperty({ description: '默认值', required: false })
  @IsOptional()
  @IsString({ message: '默认值必须为字符串' })
  defaultValue?: string;

  @ApiProperty({ description: '字段排序', required: false })
  @IsOptional()
  sortOrder?: number;

  @ApiProperty({ description: '验证规则（正则表达式）', required: false })
  @IsOptional()
  @IsString({ message: '验证规则必须为字符串' })
  validationRule?: string;

  @ApiProperty({ description: '验证错误提示', required: false })
  @IsOptional()
  @IsString({ message: '验证错误提示必须为字符串' })
  validationMessage?: string;
}

export class CreateFormTemplateDto {
  @ApiProperty({ description: '表单名称' })
  @IsNotEmpty({ message: '表单名称不能为空' })
  @IsString({ message: '表单名称必须为字符串' })
  formName: string;

  @ApiProperty({ description: '表单描述', required: false })
  @IsOptional()
  @IsString({ message: '表单描述必须为字符串' })
  formDescription?: string;

  @ApiProperty({ description: '表单字段列表', type: [CreateFormFieldDto] })
  @IsArray({ message: '表单字段必须为数组' })
  @ValidateNested({ each: true })
  @Type(() => CreateFormFieldDto)
  fields: CreateFormFieldDto[];

  @ApiProperty({ description: '表单状态（启用/禁用）', required: false })
  @IsOptional()
  @IsBoolean({ message: '表单状态必须为布尔值' })
  isActive?: boolean;

  @ApiProperty({ description: '创建者ID', required: false })
  @IsOptional()
  @IsString({ message: '创建者ID必须为字符串' })
  createdBy?: string;

  @ApiProperty({ description: '表单唯一标识符', required: false })
  @IsOptional()
  @IsString({ message: '表单标识符必须为字符串' })
  formKey?: string;
}